package {
	import flash.media.Sound;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	import flash.events.Event;

	public class ScreenGame extends Screen2 {
		private var mapalab:MapaLab;
		public static const PIXEL_SIZE:uint = 100;
		private var myArrObject:Array = new Array();
		private var myBloques:Array = new Array();
		private var myCoins:Array = new Array();
		private var myTimes:Array = new Array();
		private var myTorchs:Array = new Array();
		private var myTrapsH:Array = new Array();
		private var ari:Ari;
		private var t:uint = 0;
		private var hitCoin:Boolean;
		private var hitTime:Boolean;
		private var countCoins:uint = 0;
		private var pressed:Array = new Array(4);
		public var pause:Boolean;
		private var sound:Boolean;
		private var hit:Array = new Array(4);
		public const BITMAP_SIZE:uint = 200;
		private var toolbar:Toolbar = new Toolbar();
		private var countFrame:uint = 0;
		private var hitTorch:Boolean;
		private var hitEnd:Boolean = false;
		private var hitTrap:Boolean = false;
		[Embed(source='../sounds/hitTorchSound.mp3')]
		public var embeddedSound:Class;
		[Embed(source='../sounds/hitCoinSound.mp3')]
		public var embeddedSound1:Class;
		[Embed(source='../sounds/hitTimeSound.mp3')]
		public var embeddedSound2:Class;
		[Embed(source='../sounds/hitSpearSound.mp3')]
		public var embeddedSound3:Class;
		public function ScreenGame() {
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		private function onAddedToStage(event:Event):void {
			ari = new Ari();
			ari.x = 330;
			ari.y = 225;
			
			mapalab = new MapaLab(this);
			mapalab.y = -9*PIXEL_SIZE;
			mapalab.mask = ari.aura;
			addChild(mapalab);
			addChild(ari);
			ari.scaleX = PIXEL_SIZE/BITMAP_SIZE;
			ari.scaleY = PIXEL_SIZE/BITMAP_SIZE;
			
			addChild(toolbar);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		private function onKeyDown(event:KeyboardEvent):void{
			switch(event.keyCode){
				case Keyboard.RIGHT: pressed[0] = true; break;
				case Keyboard.UP: pressed[1] = true; break;
				case Keyboard.LEFT: pressed[2] = true; break;
				case Keyboard.DOWN: pressed[3] = true; break;
				case Keyboard.P :
				if (pause) pause = false;
				else 
				pause = true; break;
				case Keyboard.S:
				if (sound) sound = false;
				else 
				sound = true; break;
			}
			
		}
		private function onKeyUp(event:KeyboardEvent):void {
			switch (event.keyCode) {
				case Keyboard.RIGHT:pressed[0] = false; break;
				case Keyboard.UP:   pressed[1] = false; break;
				case Keyboard.LEFT: pressed[2] = false; break;
				case Keyboard.DOWN: pressed[3] = false; break;
			}
		}
		private function animateAri():void{
			var period:Number = 10;
			t++;
			if (t == period) {
				t = 0;
			}
			if ( t < period/2) {
				ari.y += 2;
			} else {
				ari.y -= 2;
			}
		}
		
		
		private function endOfGame():void{
			stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			laberintoapp.gotoScreen("end");
		}
		
		private function makeTimer():void{
			countFrame++;
				if (countFrame == stage.frameRate){
					if (toolbar.secs>0) toolbar.secs--;
					else toolbar.secs = 0;
					toolbar.time.text = 'Tiempo: ' + toolbar.secs;
					countFrame = 0;
					if (toolbar.secs == 0){
						stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
						laberintoapp.gotoScreen("over");
					}
				}
		}
		private function detectWalls():void{
			for (var h:uint = 0; h < hit.length; h++){
				for (var i:uint = 0; i < myBloques.length; i++){
					hit[h] = ari.coll[h].hitTestObject(myBloques[i]);
					if (hit[h]){
						break;
					}
				}
			}
		}
		private function movementKeys():void{
			for (var i:uint = 0; i < pressed.length ; i++){
				if (pressed[i] && !hit[i]) {
					switch (i){
						case 0: mapalab.x -= mapalab.speed;
								ari.img.scaleX = 1;
								break;
						case 1: mapalab.y += mapalab.speed; break;
						case 2: mapalab.x += mapalab.speed;
								ari.img.scaleX = -1;
							 	break;
						case 3: mapalab.y -= mapalab.speed; break;
					}
	  			animateAri();
				}
			}
		}
		private function objectsInteraction():void{
		for (var i:uint = 0; i < myCoins.length; i++){
					hitCoin = ari.img.hitTestObject(myCoins[i].aura);
					if (hitCoin){
						if (!sound){
							var soundClip1:Sound = new embeddedSound1();		
							soundClip1.play();
						}
					    countCoins++;
						toolbar.score.text = 'Monedas: ' + countCoins.toString();
						mapalab.removeChild(myCoins[i]);
						myCoins.splice(i,1);
					}
				}
				for (i = 0; i < myTimes.length; i++){
					hitTime = ari.img.hitTestObject(myTimes[i].aura);
					if (hitTime){
						if (!sound){
							var soundClip2:Sound = new embeddedSound2();		
							soundClip2.play();
						}
						const addTime:uint = 15;
					    toolbar.secs += addTime;
						toolbar.score.setTextFormat(toolbar.format);
						toolbar.time.text = 'Tiempo: ' + toolbar.secs;
						mapalab.removeChild(myTimes[i]);
						myTimes.splice(i,1);
					}
					
				}
				for (i = 0; i < myTorchs.length; i++){
					hitTorch = ari.img.hitTestObject(myTorchs[i].aura);
					if (hitTorch){
						if (!sound){
							var soundClip:Sound = new embeddedSound();		
							soundClip.play();
						}
						ari.radio += 50;
						ari.aura.visible = false;
						ari.drawAura(ari.radio);
						mapalab.mask = ari.aura;
						mapalab.removeChild(myTorchs[i]);
						myTorchs.splice(i,1);
					}					
				}
				for (i = 0; i < myTrapsH.length; i++){
					hitTrap = ari.img.hitTestObject(myTrapsH[i].aura);
					if (hitTrap){
						if (!sound){
							var soundClip3:Sound = new embeddedSound3();
							soundClip3.play();
						}
						mapalab.x = 0;
						mapalab.y = -9*PIXEL_SIZE;
					}					
				}	
		}
		private function onEnterFrame(e:Event):void{
			if (!pause){
				
				makeTimer();	
				detectWalls();
				movementKeys();
				objectsInteraction();


				hitEnd = ari.img.hitTestObject(mapalab.end);
				if (hitEnd){
					endOfGame();
				}
								
			}				
		}
		public function get pool():Array{
			return myArrObject;	
		}
		public function set pool(value:Array):void{
			myArrObject = value;
		}
		
		public function get bloques():Array{
			return myBloques;	
		}
		public function set bloques(value:Array):void{
			myBloques = value;
		}
		public function get coins():Array{
			return myCoins;	
		}
		public function set coins(value:Array):void{
			myCoins = value;
		}
		public function get times():Array{
			return myTimes;	
		}
		public function set times(value:Array):void{
			myTimes = value;
		}
		public function get torchs():Array{
			return myTorchs;	
		}
		public function set torchs(value:Array):void{
			myTorchs = value;
		}
		public function get trapsH():Array{
			return myTrapsH;	
		}
		public function set trapsH(value:Array):void{
			myTrapsH = value;
		}
		public function get pauseTo():Boolean{
			return pause;	
		}
		public function set pauseTo(value:Boolean):void{
			pause = value;
		}
		
	}	
}
